package com.ebc.eservice.controller.app;

import com.ebc.eservice.pojo.pic.UploadFile;
import com.ebc.eservice.service.EbcCardService;
import com.ebc.eservice.service.app.AppArticleService;
import com.ebc.eservice.service.app.AppIntroductService;
import com.ebc.eservice.util.JsonResult;
import com.ebc.eservice.util.common.constant.ApiTextHelperConstant;
import com.ebc.eservice.util.common.constant.DeveloperConstant;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

/**
 * @author: Joey
 * @Description:个性化简介[公司简介]+个性化内容[文章]
 * @date:2022/9/25 15:13
 */
@Api(tags = "小程序-[个性化简介+个性化内容]")
@RestController
@RequestMapping("/appCompany")
public class CompanyProfileArticleController {

    @Autowired
    private AppArticleService appArticleService;
    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private AppIntroductService appIntroductService;

    /********************************************************************
     *
     *      个性化 - 简介
     *
     * *******************************************************************/
    @ApiOperation(value = "app-获取个性化简介", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @GetMapping("/getIntro")
    public JsonResult getCompanyList(Integer companyId){
        return appIntroductService.getIntroList(companyId);
    }

    @ApiOperation(value = "获取个性简介照片显示", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @GetMapping(value = "/getIntroPic",produces = {MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_GIF_VALUE}) // 支持jpg\png\gif
    @ResponseBody
    public byte[] getPic(Integer IntroId){
        System.out.println("=====正在调用简介照片:"+IntroId);
        byte[] data = null;
        //条件查询器
        Query query = Query.query(Criteria.where("introId").is(IntroId));
        UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
        if(file != null){
            data = file.getContent().getData();//blob类型
        }
        return data;
    }


    /********************************************************************
     *
     *      个性化内容 -文章
     *
     * *******************************************************************/
    @ApiOperation(value = "app-获取文章列表+照片", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @GetMapping("/getArticle")
    public JsonResult getArticleList(Integer typeId){
        Integer fg = typeId;
        if(typeId == null){
            fg = 1;
        }
        return appArticleService.getArticleList(fg);
    }
    @ApiOperation(value = "单例-获取文章标题照片", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @GetMapping(value = "/getArticlePic",produces = {MediaType.IMAGE_JPEG_VALUE,MediaType.IMAGE_PNG_VALUE, MediaType.IMAGE_GIF_VALUE}) // 支持jpg\png\gif
    @ResponseBody
    public byte[] getArticlePic(Integer articleId){
        System.out.println("=====正在调用文章照片:"+articleId);
        byte[] dataArt = null;
        //条件查询器
        Query query = Query.query(Criteria.where("articleId").is(articleId));
        UploadFile file = mongoTemplate.findOne(query,UploadFile.class);
        if(file != null){
            dataArt = file.getContent().getData();//blob类型
        }
        return dataArt;
    }
    @ApiOperation(value = "文章浏览量增加", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @GetMapping("/addViewNum")
    public void addViewNum(Integer articleId){
        appArticleService.addViewNum(articleId);
    }


    @ApiOperation(value = "TEST-获取文章分类", notes = ApiTextHelperConstant.DEVELOPER + DeveloperConstant.FH)
    @GetMapping("/getArticleType")
    public JsonResult getArticleTypeList(){
        return appArticleService.getArticleType();
    }

}
